//
//  SelectPlaceTableViewController.m
//  ChatKit-OC
//
//  Created by th on 2017/3/26.
//  Copyright © 2017年 ElonChan. All rights reserved.
//

#import "SelectPlaceTableViewController.h"

#import "PlaceCell.h"

@interface SelectPlaceTableViewController ()

@end

@implementation SelectPlaceTableViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self.navigationController setNavigationBarHidden:NO];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    
    self.title = @"搜索结果";
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    NSMutableArray *datas = @[].mutableCopy;
    
    [_datas enumerateObjectsUsingBlock:^(CLPlacemark *obj, NSUInteger idx, BOOL * _Nonnull stop) {
        PlaceModel *md = [PlaceModel new];
        
        md.place = obj.name;
        
        NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:md.place];
        text.font = [UIFont systemFontOfSize:16];
        text.color = [UIColor blackColor];
        
        YYTextContainer *container = [YYTextContainer containerWithSize:CGSizeMake(kMainWidth - kSpaceX*2, HUGE)];
        container.maximumNumberOfRows = 0;
        
        YYTextLayout *layout = [YYTextLayout layoutWithContainer:container text:text];
        
        md.layout = layout;
        
        md.location = obj.location;
        
        [datas addObject:md];
    }];
    
    _datas = datas;
    NSLog(@"------完成");
    [self.tableView reloadData];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return _datas.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    PlaceCell *cell = [PlaceCell cellWithTalbeView:tableView];
    
    // Configure the cell...
    
    cell.model = _datas[indexPath.row];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if ([self.delegate respondsToSelector:@selector(SelectPlaceDelegateWithLocation: placeName:)]) {
        PlaceModel *md = _datas[indexPath.row];
        [self.delegate SelectPlaceDelegateWithLocation:md.location placeName:md.place];
    }
    
    [self.navigationController popViewControllerAnimated:YES];
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    return ((PlaceModel *)_datas[indexPath.row]).layout.textBoundingSize.height + 20;
}
 

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
